package com.cenntro.oss.service.impl;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import com.cenntro.oss.common.constants.ImportDataConstants;
import com.cenntro.oss.mapper.DevDeviceStaticMapper;
import com.cenntro.oss.model.dbo.DevCertificateDO;
import com.cenntro.oss.model.dbo.DevDeviceStaticDO;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

@Component
public class ValidExcel {

	@Resource
	private DevDeviceStaticMapper devDeviceStaticMapper;

	public void valid() {

	}

	/**
	 * 校验基本数据导入合法性
	 * 
	 * @param list
	 * @param color
	 * @param mapCarType
	 * @param mapPlant
	 * @param type
	 *            1插入 2更新
	 */
	@SuppressWarnings("deprecation")
    public void validDevice(List<DevDeviceStaticDO> list, List<String> color, List<String> unlockWay, Map<String, Integer> mapCarType, List<String> plant, int type) {
		LinkedHashSet<String> snSet = Sets.newLinkedHashSet(); // 序列号
		LinkedHashSet<String> vinSet = Sets.newLinkedHashSet(); // vincode
		LinkedHashSet<String> imeiSet = Sets.newLinkedHashSet(); // vincode
		LinkedHashSet<String> tboxImeiSet = Sets.newLinkedHashSet(); // vincode
		LinkedHashSet<String> bmsNoSet = Sets.newLinkedHashSet(); // vincode
		// Stream.iterate(0, i -> i+1).forEach(i->{
		int i = ImportDataConstants.START_ROW + 1;
		for (DevDeviceStaticDO dsd : list) {
			i++;
			boolean devSnResult = snSet.add(dsd.getDevSn());
			Assert.isTrue(devSnResult, ImportDataConstants.splicing(i, ImportDataConstants.DEV_SN_TABLE_REPEAT)); // 行序列号重复
			if (StringUtils.isNotBlank(dsd.getVin())) {
				boolean vinSetResult = vinSet.add(dsd.getVin());
				Assert.isTrue(vinSetResult, ImportDataConstants.splicing(i, ImportDataConstants.CARNUB_TABLE_REPEAT)); // vincode号重复
			}
			boolean imeiSetResult = imeiSet.add(dsd.getImei());
			Assert.isTrue(imeiSetResult, ImportDataConstants.splicing(i, ImportDataConstants.IMEI_TABLE_REPEAT));
			boolean tboxSetResult = tboxImeiSet.add(dsd.getTbox());
			Assert.isTrue(tboxSetResult, ImportDataConstants.splicing(i, ImportDataConstants.TBOX_TABLE_REPEAT));

			Assert.notNull(dsd.getDevSn(), ImportDataConstants.splicing(i, ImportDataConstants.DEV_SN_IS_NULL));
			Assert.isTrue(dsd.getDevSn().matches("[0-9a-zA-z]{1,50}"), ImportDataConstants.splicing(i, ImportDataConstants.DEV_SN_FORMAT_ERROR));
			if (StringUtils.isNotBlank(dsd.getVin())) {
				Assert.isTrue(dsd.getVin().matches("^[0-9a-zA-z]{17}$"), ImportDataConstants.splicing(i, ImportDataConstants.CARNUB_FORMAT_ERROR));
			}

			Assert.notNull(dsd.getColor(), ImportDataConstants.splicing(i, ImportDataConstants.COLOR_IS_NULL));
			Assert.isTrue(color.indexOf(dsd.getColor()) > -1, ImportDataConstants.splicing(i, ImportDataConstants.COLOR_UNMATCH));

			Assert.notNull(mapCarType.get(dsd.getCarType()), ImportDataConstants.splicing(i, ImportDataConstants.CAR_ID_IS_NULL));

			Assert.isTrue(mapCarType.containsKey(dsd.getCarType()), ImportDataConstants.splicing(i, ImportDataConstants.CAR_ID_UNMATCH));

			Assert.notNull(dsd.getDapName(), ImportDataConstants.splicing(i, ImportDataConstants.PLANT_IS_NULL));
			Assert.isTrue(plant.indexOf(dsd.getDapName()) > -1, ImportDataConstants.splicing(i, ImportDataConstants.PLANT_IS_UNMATCH));

			Assert.notNull(dsd.getBuildTime(), ImportDataConstants.splicing(i, ImportDataConstants.BUILDTIME_IS_NULL));
			Assert.isTrue(dsd.getBuildTime().getYear() > 70, ImportDataConstants.splicing(i, ImportDataConstants.BUILDTIME_FORMAT_ERROR));

			Assert.notNull(dsd.getImei(), ImportDataConstants.splicing(i, ImportDataConstants.IMEI_IS_NULL));
			Assert.isTrue(dsd.getImei().matches("^\\d{15}$"), ImportDataConstants.splicing(i, ImportDataConstants.IMEI_FORMAT_ERROR));

			Assert.notNull(dsd.getTbox(), ImportDataConstants.splicing(i, ImportDataConstants.TBOX_IS_NULL));
			Assert.isTrue(dsd.getTbox().matches("^\\d{15}$"), ImportDataConstants.splicing(i, ImportDataConstants.TBOX_FORMAT_ERROR));

			Assert.notNull(dsd.getBomVersion(), ImportDataConstants.splicing(i, ImportDataConstants.BOM_VERSION_IS_NULL));
			Assert.isTrue(dsd.getBomVersion().matches("^.{1,50}$"), ImportDataConstants.splicing(i, ImportDataConstants.BOM_VERSION_FORMAT_ERROR));

			Assert.notNull(dsd.getGreenListNum(), ImportDataConstants.splicing(i, ImportDataConstants.GREEN_LIST_NUM_IS_NULL));
			Assert.isTrue(dsd.getGreenListNum().matches("^.{1,50}$"), ImportDataConstants.splicing(i, ImportDataConstants.GREEN_LIST_NUM_FORMAT_ERROR));

			Assert.notNull(dsd.getUnlockWay(), ImportDataConstants.splicing(i, ImportDataConstants.UNLOCK_WAY_IS_NULL));
			Assert.isTrue(unlockWay.indexOf(dsd.getUnlockWay()) > -1, ImportDataConstants.splicing(i, ImportDataConstants.UNLOCK_WAY_UNMATCH));

			if (StringUtils.isNotBlank(dsd.getRemarks())) {
				Assert.isTrue(dsd.getRemarks().matches("^.{0,255}$"), ImportDataConstants.splicing(i, ImportDataConstants.REMARKS_FORMAT_ERROR));
			}
			Assert.notNull(dsd.getBmsNo(), ImportDataConstants.splicing(i, ImportDataConstants.BMSNO_IS_NULL));
			boolean add = bmsNoSet.add(dsd.getBmsNo());
			Assert.isTrue(add, ImportDataConstants.splicing(i, ImportDataConstants.BMSNO_TABLE_REPEAT));

		}
		String index = "";
		if (type == 1) {
			// 判断数据是否重复
			List<Map<String, String>> devSnList = devDeviceStaticMapper.selectPrimary(ImmutableMap.of("devSnList", snSet));
			// 判断数据是否和数据库中的值重复
			List<Map<String, String>> imeiList = devDeviceStaticMapper.selectPrimary(ImmutableMap.of("imeiList", imeiSet));
			// 判断数据是否重复
			List<Map<String, String>> tboxList = devDeviceStaticMapper.selectPrimary(ImmutableMap.of("tboxList", tboxImeiSet));
			// // 判断数据是否重复
			List<Map<String, String>> bmsNoList = devDeviceStaticMapper.selectPrimary(ImmutableMap.of("bmsNoList", bmsNoSet));
			index = getIndex(Lists.newArrayList(imeiSet), imeiList, 1);
			Assert.isTrue(index.length() == 0, ImportDataConstants.splicing(index, ImportDataConstants.IMEI_EXIST));
			index = getIndex(Lists.newArrayList(tboxImeiSet), tboxList, 2);
			Assert.isTrue(index.length() == 0, ImportDataConstants.splicing(index, ImportDataConstants.TBOX_EXIST));
			index = getIndex(Lists.newArrayList(bmsNoSet), bmsNoList, 3);
			Assert.isTrue(index.length() == 0, ImportDataConstants.splicing(index, ImportDataConstants.BMSNO_EXIST));
			index = getIndex(com.google.common.collect.Lists.newArrayList(snSet), devSnList, 4);
			Assert.isTrue(index.length() == 0, ImportDataConstants.splicing(index, ImportDataConstants.DEV_SN_EXIST));
		} else if (type == 2) {
			// Assert.isTrue(index.length() != 0,
			// ImportDataConstants.splicing(index,
			// ImportDataConstants.DEV_SN_UNMATCH));
		}

	}

	/**
	 * 找到下标，行号 type 1 imei 2 tbox 3vincode 4 devsn
	 * 
	 * @param list
	 *            源数据集合 result 已经存在数据集合
	 * @return
	 */
	private String getIndex(List<String> sourceList, List<Map<String, String>> result, int type) {
		String index = "";
		for (Map<String, String> map : result) {
			String str = "";
			if (type == 1) {
				str = map.get("imei");
			}
			if (type == 2) {
				str = map.get("tbox");
			}
			if (type == 3) {
				str = map.get("bmsNo");
			}
			if (type == 4) {
				str = map.get("dev_sn");
			}
			int i = sourceList.indexOf(str) + 3;
			index = index.length() > 0 ? index + "," : "";
			index += i;
			if (sourceList.indexOf(str) >= 0) {
				return index;
			}
		}
		return index;
	}

	/**
	 * 合格证信息检查
	 * 
	 * @param list
	 */
	public void validCertificate(List<DevCertificateDO> list) {
		LinkedHashSet<String> snSet = Sets.newLinkedHashSet(); // 序列号
		LinkedHashSet<String> certificateSnSet = Sets.newLinkedHashSet(); // vincode
		LinkedHashSet<String> paperNumSet = Sets.newLinkedHashSet(); // vincode
		LinkedHashSet<String> labelNumSet = Sets.newLinkedHashSet(); // vincode
		LinkedHashSet<String> conformity = Sets.newLinkedHashSet(); // vincode
		// Stream.iterate(0, j -> j + 1).forEach(j -> {
		int i = ImportDataConstants.START_ROW + 1;
		for (DevCertificateDO cert : list) {
			i++;
			Assert.notNull(cert.getDevSn(), ImportDataConstants.splicing(i, ImportDataConstants.DEV_SN_IS_NULL));
			Assert.notNull(cert.getDateIssue(), ImportDataConstants.splicing(i, ImportDataConstants.DATE_ISSUE_IS_NULL));
			Assert.isTrue(cert.getDateIssue().getYear() > 70, ImportDataConstants.splicing(i, ImportDataConstants.DATE_ISSUE_FORMAT_ERROR));

			Assert.isTrue(cert.getCertificateSn().matches("[0-9a-zA-z]{1,50}"), ImportDataConstants.splicing(i, ImportDataConstants.CERT_FORMAT_ERROR));
			Assert.notNull(cert.getCertificateSn(), ImportDataConstants.splicing(i, ImportDataConstants.CERT_IS_NULL));
			Assert.notNull(cert.getPaperNum(), ImportDataConstants.splicing(i, ImportDataConstants.PAPER_NUM_IS_NULL));
			Assert.isTrue(cert.getPaperNum().matches("^.{1,50}$"), ImportDataConstants.splicing(i, ImportDataConstants.PAPER_NUM_FORMAT_ERROR));
			Assert.notNull(cert.getLabelNum(), ImportDataConstants.splicing(i, ImportDataConstants.LABEL_NUM_IS_NULL));
			Assert.isTrue(cert.getLabelNum().matches("^.{1,50}$"), ImportDataConstants.splicing(i, ImportDataConstants.LABEL_NUM_FORMAT_ERROR));
			Assert.notNull(cert.getCertificateConformity(), ImportDataConstants.splicing(i, ImportDataConstants.CONFORMITY_IS_NULL));
			Assert.isTrue(cert.getCertificateConformity().matches("^.{1,50}$"), ImportDataConstants.splicing(i, ImportDataConstants.CONFORMITY_FORMAT_ERROR));

			if (StringUtils.isNotBlank(cert.getRemarks())) {
				Assert.isTrue(cert.getRemarks().matches("^.{0,255}$"), ImportDataConstants.splicing(i, ImportDataConstants.REMARKS_FORMAT_ERROR));
			}

			boolean devSnResult = snSet.add(cert.getDevSn());
			Assert.isTrue(devSnResult, ImportDataConstants.splicing(i, ImportDataConstants.DEV_SN_TABLE_REPEAT)); // 行序列号重复

			boolean certificateSnResult = certificateSnSet.add(cert.getCertificateSn());
			Assert.isTrue(certificateSnResult, ImportDataConstants.splicing(i, ImportDataConstants.CERT_TABLE_REPEAT)); // 行序列号重复

			boolean paperNumResult = paperNumSet.add(cert.getPaperNum());
			Assert.isTrue(paperNumResult, ImportDataConstants.splicing(i, ImportDataConstants.PAPER_NUM_TABLE_REPEAT));

			boolean labelNumResult = labelNumSet.add(cert.getLabelNum());
			Assert.isTrue(labelNumResult, ImportDataConstants.splicing(i, ImportDataConstants.LABEL_NUM_TABLE_REPEAT));

			boolean conformityResult = conformity.add(cert.getCertificateConformity());
			Assert.isTrue(conformityResult, ImportDataConstants.splicing(i, ImportDataConstants.CONFORMITY_TABLE_REPEAT));
		}

	}

}
